<?php

/*

  Action - Reaction	$2

  Discard any number of cards. +$1 per card discarded.
  When another player plays an Attack card, you may reveal this from your hand.
  If you do, +2 cards, then put 2 cards from your hand on top of your deck.

 */

class SecretChamber extends Card implements iAction, iKingdom, iReaction, iIntrigue, iReactingCard {

    private $reactConditions;

    public function __construct() {
        parent::__construct("Secret Chamber", 2);
        $this->undoPlay = false;
        $this->undoBuy = true;

        $this->reactConditions = new ReactionConditions();
        $this->reactConditions->action = array(Attack);
        $this->reactConditions->actor = array(SomeoneElse);
        $this->reactConditions->cardLocation = array(PlayHand);
        $this->reactConditions->limit = UnlimitedReaction;
    }

    public function getDescription() {
        return array(
            "Discard any number of cards. +$1 per card discarded.",
            "When another player plays an Attack card, you may reveal this from your hand.  If you do, +2 cards, then put 2 cards from your hand on top of your deck.");
    }

    public function play(Game $game, $isRealCard) {

        $cards = $game->getPlayHand();
        if (sizeof($cards) > 0) {

            $output = MySocket::GetUserReply($game, Input_ChooseMany, $cards, 0, -1, null, "Choose cards to discard");
            $discardedCards = 0;
            foreach ($output as $card_name) {
                $game->moveCardFromPileToPile($card_name, PlayHand, DiscardPile);
                $discardedCards++;
            }
            ScreenDisplay::addScreenMessage($game, true, "discard " . nicePrint($output) . " and get +$discardedCards$", "discards " . sizeof($output) . " cards and gets +$discardedCards$");

            $game->addMoney(new Price($discardedCards));
        }

        $game->cardActionFinished();
        return true;
    }

    public function getConditions() {
        return $this->reactConditions;
    }

    public function isReactRelevant(Game $game, $card, $name) {
        return true;
    }

    public function react(Game $game, $card, $name) {
        ScreenDisplay::addScreenMessage($game, true, "reveal " . nicePrint($this), "reveals " . nicePrint($this), $name);

        $drawed_cards = $game->drawCards(2, $name);
        ScreenDisplay::addScreenMessage($game, true, "draw " . nicePrint($drawed_cards), "draws " . sizeof($drawed_cards) . " cards", $name);

        $playHand = $game->getPlayHand($name);

        if (sizeof($playHand) > 2) {
            $output = MySocket::GetUserReply($game, Input_ChooseMany, $playHand, 2, 2, null, "Choose 2 cards to return to deck", $name);

            $return_cards_obj = array();
            foreach ($output as $p) {
                $return_cards_obj[] = n2c($p);
            }
            $output = MySocket::GetUserReply($game, Input_Reorder, $return_cards_obj, -1, -1, null, "Choose the order", $name);
            ScreenDisplay::addScreenMessage($game, false, " and return " . nicePrint($output) . "on top of your deck", " and returns " . sizeof($output) . " cards ot top of his deck", $name);
            $output = array_reverse($output);
            foreach ($output as $p) {
                $game->moveCardFromPileToPile($p, PlayHand, DrawPile, $name);
            }
        } else if (sizeof($playHand) > 0) {
            $output = MySocket::GetUserReply($game, Input_Reorder, $playHand, -1, -1, null, "Choose the order", $name);
            ScreenDisplay::addScreenMessage($game, false, " and return " . nicePrint($output) . "on top of your deck", " and returns " . sizeof($output) . " cards ot top of his deck", $name);
            $output = array_reverse($output);
            foreach ($output as $p) {
                $game->moveCardFromPileToPile($p, PlayHand, DrawPile, $name);
            }
        }

        $game->setTempCard($card);
        return true;
    }

}

?>